package net.ojbk.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.ojbk.database.DatabaseConnection;
import net.ojbk.po.ModifyPassword;

public class ModifyPasswordDao {
	static final String tableName = "modifyPassword";//数据库表名称
	DatabaseConnection dbconn; // 数据库连接控制对象
	Statement stmt; // sql语句 操作对象
	public ModifyPasswordDao(){
		dbconn =new DatabaseConnection();
		stmt=dbconn.getStatement();
	}
	public void finalize()
	{
		dbconn.close();
	}
	public boolean addOneRecord(ModifyPassword mp)
	{
		//insert into modifyPassword(email,code)value('1531764720','0000')
		boolean flag=false;
		try {
			String sql="insert into "
					+ tableName
					+ "(email,code) value('"
					+ mp.getEmail()
					+ "','"
					+ mp.getCode()
					+ "')";
			stmt.executeUpdate(sql);
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public boolean updateOneRecord(ModifyPassword mp)
	{
		//update modifyPassword set code='1111' where email='1531764720'
		boolean flag=false;
		try {
			String sql="update "
					+ tableName
					+ " set code='"
					+ mp.getCode()
					+ "' where email='"
					+ mp.getEmail()
					+ "'";
			stmt.executeUpdate(sql);
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public boolean queryOneRecord(ModifyPassword mp)
	{
		//select * from modifyPassword where email='1531764720'
		boolean flag=false;
		ResultSet re;
		try {
			String sql="select * from "
					+ tableName
					+ " where email='"
					+ mp.getEmail()
					+"' or code='"
					+mp.getCode()
					+ "'";
			re=stmt.executeQuery(sql);
			if(re.next()){
				mp.setEmail(re.getString("email"));
				mp.setCode(re.getString("code"));
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public boolean deleteOneRecord(ModifyPassword mp)
	{
		//delete from modifyPassword where email='1531764720'
		boolean flag=false;
		try {
			String sql="delete from "
					+ tableName
					+ " where email='"
					+ mp.getEmail()
					+ "'";
			stmt.executeUpdate(sql);
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public boolean checkOneRecord(String code, String email) {
		boolean flag = false;
		ResultSet re;
		try {
			String sql="select * from '"
					+ tableName
					+ "' where id='"
					+ code
					+ "' or email='"
					+ email
					+ "';";
			re = stmt.executeQuery(sql);
			if (re.next()) {
				flag = true;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
		
	}
	public static void main(String[] args)
	{
	}
}
